package com.tinklabs.demo.bean;

import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jennifer on 2018/4/14.
 */

public class CityGuideResult {

    private List<Item> dataList;

    public List<Item> getDataList() {
        return dataList;
    }

    public static class Item extends CityGuideResult {
        private int type;
        private String res;

        public Item(int type, String res) {
            this.type = type;
            this.res = res;
        }

        private Item() {
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getRes() {
            return res;
        }

        public void setRes(String res) {
            this.res = res;
        }

        @Override
        public String toString() {
            return "Item{" +
                    "type=" + type +
                    ", res='" + res + '\'' +
                    '}';
        }
    }

    public static class NormalItem extends Item {
        private String storeName;
        private String description;

        public NormalItem(int type, String res) {
            super(type, res);
        }

        public NormalItem(int type, String res, String storeName, String description) {
            super(type, res);
            this.storeName = storeName;
            this.description = description;
        }

        private NormalItem() {
            super();
        }

        public String getStoreName() {
            return storeName;
        }

        private void setStoreName(String storeName) {
            this.storeName = storeName;
        }

        public String getDescription() {
            return description;
        }

        private void setDescription(String description) {
            this.description = description;
        }

        @Override
        public String toString() {
            return "NormalItem{" +
                    ", storeName='" + storeName + '\'' +
                    ", description='" + description + '\'' +
                    '}';
        }
    }

    public static class TypeModelDeserializer implements JsonDeserializer<CityGuideResult> {

        @Override
        public CityGuideResult deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {

            List<Item> list = new ArrayList<>();

            JsonArray jsonArray = json.getAsJsonArray();

            for (JsonElement element : jsonArray) {
                JsonObject jsonObject = element.getAsJsonObject();
                JsonElement jsonType = jsonObject.get("type");
                int type = jsonType.getAsInt();

                Item item;
                if (1 == type) {
                    NormalItem typeModel = new NormalItem();
                    typeModel.setStoreName(jsonObject.get("storeName").getAsString());
                    typeModel.setDescription(jsonObject.get("description").getAsString());

                    item = typeModel;
                } else {
                    item = new Item();
                }

                item.setType(jsonObject.get("type").getAsInt());
                item.setRes(jsonObject.get("res").getAsString());

                list.add(item);
            }

            CityGuideResult result = new CityGuideResult();
            result.dataList = list;


            return result;
        }
    }


}
